<?php

/***********************************INFO************************************** *
 * Class Name: GameMgrDef
 * Author: cf.Studios
 * Description: 
 * **************************************************************************** */

class GameMgrDef {
    
    private $players,
            $phase;
/************************************************************************** *
 * *********************************CONSTRUCTOR!**************************** *
 * Author: Nicholas Malacarne
 * Parameters: $firstPlayer- The name of the first player.
 *             $secondPlayer - The name of the second player.
 * Description: Construcs the 
 * ************************************************************************ */
    function __construct($firstPlayer, $secondPlayer, $maxLife)
    {
        $this->players = array();
        array_push($this->players, new PlayerDef($maxLife, $firstPlayer));
        array_push($this->players, new PlayerDef($maxLife, $secondPlayer));
        
//        $this->phase = array("negEffects" => array("bPhase" => false,
//                                                   "name" => "Assign Negative 
//                                                             Effects"),
//                             "draw" => array("bPhase" => false,
//                                             "name" => "Draw"),
//                             "spirit" => array("bPhase" => false,
//                                               "name" => "Spirit"),
//                             "main" => array("bPhase" => false,
//                                             "name" => "Main"),
//                             "posEffects" => array("bPhase" => false,
//                                                   "name" => "Assign Positive
//                                                              Effects"),
//                             "decay" => array("bPhase" => false,
//                                              "name" => "Decay Effects"));
                             
    }


/**********************************FUNCTION******************************* *
 * Function Name: 
 * Author: Nicholas Malacarne
 * Parameters: $handSize - The maximum hand size allowed.
 * Description: Rolls for each player to see who goes first. Then draws a 
 * full hand of cards for each player.
 * ************************************************************************ */
    function startGame($handSize)
    {
        $report = "";
        
        do
        {
           /* Rolls a die for each player.
            */foreach ($this->players as $player)
              {
                  $player->rollDie();
              }
        }
        while($this->players[0]->getRoll() == $this->players[1]->getRoll());
    
     /* The player who rolled higher goes first.
      */if ($this->players[0]->getRoll() > $this->players[1]->getRoll())
        {
            $this->players[0]->setTurn(true);

            foreach ($this->players as $player)
            {
             /* What did each player roll?
              */$report .= $player->getName()." rolled a ".
                           $player->getRoll()."!\n";
            }
            
         /* Which player takes the first turn?
          */$report .= $this->players[0]->getName()." plays first!\n";
        }
        else
        {
            $this->players[1]->setTurn(true);

            foreach ($this->players as $player)
            {
                $report .= $player->getName()." rolled a ".
                           $player->getRoll()."!\n";
            }
            
            $report .= $this->players[1]->getName()." plays first!\n";
        }
        
     /* Displays which player's turn it is.
      */foreach ($this->players as $player)
        {
            if ($player->isTurn())
            {
                $report .= "\nIt's now ".$player->getName()."'s turn!";
            }
        }
        
        foreach ($this->players as $player)
        {
         /* Draws a hand of cards for the player.
          */$player->drawCards($handSize);
        }

        return $report;
    }

/**********************************FUNCTION******************************* *
 * Function Name: getPlayers
 * Author: Nicholas Malacarne
 * Parameters: NONE
 * Description: Returns the players array.
 * ************************************************************************ */
    function getPlayers()
    {
        return $this->players;
    }
    
/**********************************FUNCTION********************************** *
* Function Name: playCard
* Author: Nicholas Malacarne
* Parameters: $playedCard - The card last played.
* Description: Selects the card that was last played, removes it from the
* player's hand and assigns it's effects.
***************************************************************************** */
    function playCard($playedCard)
    {
        $report = "";

        foreach ($this->players as $player)
        {
            if ($player->isTurn())
            {
             /* Copies the card.
              */$theCard = array_slice($player->getHand(), $playedCard, 1);
                
             /* Removes the card from the player's hand.
              */$player->removeCard($playedCard);
                    
                $report .= $player->getName()." invokes a Glyph of ".
                           $theCard[0]->getName()."!\n";
                
                $report .= "Card effects go here!\n";
            }
        }
        
        return $report;
        
/*TODO: Assign effects!*/
//        foreach ($this->players as $player)
//        {
//            if ($player->isTurn())
//            {
//             /* Updates the player's status according to the cards effects.
//              */$update = $player->updateStatus($theCard[0]);
//
//             /* If an effect was assigned.
//              */if (!$update == null)
//                {
//                    $HTML->addText($update);
//                }
//            }   
//        }  
//
//        foreach ($this->players as $player)
//        {
//            if (!$player->isTurn())
//            {
//                $update = $player->updateStatus($theCard[0]);
//
//                if (!$update == null)
//                {
//                    $HTML->addText($update);
//                }
//            }
//        }
//
//        return $HTML->getPage();
    }
    
/**********************************FUNCTION********************************** *
* Function Name: endTurn
* Author: Nicholas Malacarne
* Parameters: NONE
* Description: Selects the card that was last played, removes it from the
* player's hand, and assigns it's effects.
***************************************************************************** */
    function endTurn()
    {
        foreach ($this->players as $player)
        {
            if ($player->isTurn())
            {
                
            }
        }
    }
}
?>